﻿using DB.Dapper;
using Orther.Model.param;
using Project.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VOL.Entity.DomainModels;

namespace Orther.Service.Miyao
{
    public class BxgsService : BaseService<qx_bxgs>
    {
        private string mainSql = "select a.* from qx_bxgs  a left join sys_user b on a.CreateID = b.User_Id where 1=1 ";
        public List<qx_bxgs> GetPageList(BxgsParam param, Pagination pagination)
        {
            int total = 0;
            FilterClass filterClass = this.WhereFilter(param);
            List<qx_bxgs> list = DapperUtilsForMysql.PaginationZjc<qx_bxgs>(mainSql, pagination.PageIndex, pagination.PageSize, ref total, "CreateDate desc", filterClass.where, filterClass.parameters);
            pagination.TotalCount = total;
            return list;
        }
        public async Task<List<qx_bxgs>> GetList(BxgsParam param, int? topNum)
        {
            FilterClass filterClass = this.WhereFilter(param);
            List<qx_bxgs> list = await DapperUtilsForMysql.queryList<qx_bxgs>(mainSql, filterClass.parameters, "CreateDate desc", filterClass.where, topNum);
            return list;
        }

        public async Task<qx_bxgs> GetObject(BxgsParam param)
        {
            qx_bxgs entity = new qx_bxgs();
            List<qx_bxgs> list = await this.GetList(param, null);
            if (list.Count > 0)
            {
                entity = list[0];
            }
            return entity;
        }

        private FilterClass WhereFilter(BxgsParam param)
        {
            FilterClass filterClass = new FilterClass();

            if (param.Id != null)
            {
                filterClass.where = filterClass.where + " and a.Id=@Id ";
                filterClass.parameters.Add("@Id", param.Id);
            }

            //默认不查询删除的
            if (param.SerchDel == null)
            {
                filterClass.where = filterClass.where + " and a.IsDeleted = 0 ";
            }

            if (param.deptIds != null && param.deptIds.Count > 0)
            {
                string insql = "";
                for (int i = 0; i < param.deptIds.Count; i++)
                {
                    if (i == 0)
                    {
                        insql = insql + " FIND_IN_SET('" + param.deptIds[0].ToString() + "', b.DeptIds) > 0 ";
                    }
                    else
                    {
                        insql = insql + " or FIND_IN_SET('" + param.deptIds[i].ToString() + "', b.DeptIds) > 0 ";
                    }

                }

                filterClass.where = filterClass.where + " and (" + insql + ") ";
            }

            if (!string.IsNullOrEmpty(param.mingcheng))
            {
                filterClass.where = filterClass.where + " and a.mingcheng = @mingcheng ";
                filterClass.parameters.Add("@mingcheng", param.mingcheng);
            }

            if (param.NotEqualId != null)
            {
                filterClass.where = filterClass.where + " and a.Id <> @NotEqualId ";
                filterClass.parameters.Add("@NotEqualId", param.NotEqualId);
            }

            return filterClass;
        }
    }
}
